﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedUtils
{
    /// <summary>
    /// Represents additional data which is attached to an edge.
    /// </summary>
    /// <typeparam name="T">The node type</typeparam>
    /// <typeparam name="E">The edge type</typeparam>
    public class Edge<T, E>
        where T : IEquatable<T>
    {
        private E data;
        private T start;
        private T end;
        /// <summary>
        /// The attached data
        /// </summary>
        public E Data
        {
            get { return data; }
            set { data = value; }
        }
        /// <summary>
        /// The start node
        /// </summary>
        public T Start
        {
            get { return start; }
            set { start = value; }
        }
        /// <summary>
        /// The end node
        /// </summary>
        public T End
        {
            get { return end; }
            set { end = value; }
        }
        /// <param name="obj">Another object to compare to.</param>
        /// <returns>true if obj and this instance are the same type and represent the same 
        /// value; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            Edge<T, E> edge = (Edge<T, E>)obj;
            return (edge.start.Equals(start) && edge.end.Equals(end));
        }
        /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
        public override int GetHashCode()
        {
            return start.GetHashCode() ^ end.GetHashCode();
        }

        /// <returns>A string representation of an Edge.</returns>
        public override string ToString()
        {
            if (end == null)
                return start.ToString() + " -> ?";
            return start.ToString() + " -> " + end.ToString();
        }
        /// <summary>
        /// Creates a new instance of the Edge structure
        /// </summary>
        /// <param name="start">The start node</param>
        /// <param name="end">The end node</param>
        /// <param name="data">The attached data</param>
        public Edge(T start, T end, E data)
        {
            this.start = start;
            this.end = end;
            this.data = data;
        }
    }
}
